<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
      </div>
    </div>
    
    <div class="login-container">
      <div class="login-form">
        <div class="form-header">
          <div class="logo-container">
            <div class="logo-icon">
              <el-icon><CreditCard /></el-icon>
            </div>
            <h1 class="form-title">银行管理系统</h1>
          </div>
          <p class="form-subtitle">安全可靠 · 专业服务 · 智能管理</p>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form-content"
          size="large"
        >
          <el-form-item>
            <div class="user-type-container">
              <el-radio-group v-model="userType" class="user-type-selector">
                <el-radio-button label="user" class="type-btn user-btn">
                  <el-icon><User /></el-icon>
                  <span>用户登录</span>
                </el-radio-button>
                <el-radio-button label="admin" class="type-btn admin-btn">
                  <el-icon><UserFilled /></el-icon>
                  <span>管理员登录</span>
                </el-radio-button>
              </el-radio-group>
            </div>
          </el-form-item>

          <el-form-item prop="account">
            <div class="input-container">
              <el-input
                v-model="loginForm.account"
                placeholder="请输入账号"
                clearable
                class="custom-input"
              >
                <template #prefix>
                  <el-icon class="input-icon"><User /></el-icon>
                </template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item prop="password">
            <div class="input-container">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                show-password
                clearable
                class="custom-input"
                @keyup.enter="handleLogin"
              >
                <template #prefix>
                  <el-icon class="input-icon"><Lock /></el-icon>
                </template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleLogin"
              class="login-btn"
            >
              <template v-if="!loading">
                <el-icon><Right /></el-icon>
                <span>登录</span>
              </template>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>

          <div class="form-footer">
            <el-button link @click="goToRegister" class="footer-btn">
              <el-icon><Plus /></el-icon>
              还没有账号？立即注册
            </el-button>
            <el-button link @click="goToHome" class="footer-btn">
              <el-icon><HomeFilled /></el-icon>
              返回首页
            </el-button>
          </div>
        </el-form>
      </div>

      <div class="login-banner">
        <div class="banner-content">
          <div class="banner-title">
            <h2>专业的金融服务平台</h2>
            <div class="title-line"></div>
          </div>
          <p class="banner-desc">
            为您提供安全、便捷、高效的银行服务体验<br>
            随时随地管理您的资产，享受数字化金融生活
          </p>
          <div class="banner-features">
            <div class="feature-item" v-for="(feature, index) in features" :key="index">
              <div class="feature-icon">
                <el-icon>
                  <component :is="feature.icon" />
                </el-icon>
              </div>
              <div class="feature-text">
                <h4>{{ feature.title }}</h4>
                <p>{{ feature.desc }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userApi } from '@/api/user'
import { adminCustomerApi } from '@/api/customer'
import { 
  Lock, 
  CreditCard, 
  Money, 
  User, 
  UserFilled, 
  Right, 
  Plus, 
  HomeFilled,
  Shield,
  TrendCharts
} from '@element-plus/icons-vue'

export default {
  name: 'Login',
  components: {
    Lock,
    CreditCard,
    Money,
    User,
    UserFilled,
    Right,
    Plus,
    HomeFilled,
    Shield,
    TrendCharts
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const loginFormRef = ref()
    const loading = ref(false)
    const userType = ref('user')
    
    const loginForm = reactive({
      account: '',
      password: ''
    })
    
    const loginRules = {
      account: [
        { required: true, message: '请输入账号', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度至少6位', trigger: 'blur' }
      ]
    }

    const features = ref([
      {
        icon: 'CreditCard',
        title: '银行卡管理',
        desc: '便捷的卡片管理服务'
      },
      {
        icon: 'Money',
        title: '资金管理',
        desc: '安全的资金操作体验'
      },
      {
        icon: 'Shield',
        title: '安全保障',
        desc: '多重安全防护机制'
      },
      {
        icon: 'TrendCharts',
        title: '智能分析',
        desc: '专业的数据分析报表'
      }
    ])

    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      try {
        await loginFormRef.value.validate()
        loading.value = true
        
        let response
        if (userType.value === 'admin') {
          response = await adminCustomerApi.login(loginForm)
        } else {
          response = await userApi.login(loginForm)
        }
        
        // 检查响应结构
        if (response && response.code === 0) {
          const { data } = response
          
          // 根据用户类型适配不同的字段名
          let userInfo
          if (userType.value === 'admin') {
            // 管理员返回字段: userId, userName, imageUrl, token
            userInfo = {
              id: data.userId,
              name: data.userName,
              avatar: data.imageUrl,
              token: data.token
            }
          } else {
            // 用户返回字段: userid, username, imageurl, token  
            userInfo = {
              id: data.userid,
              name: data.username,
              avatar: data.imageurl,
              token: data.token
            }
          }
          
          // 存储用户信息到store
          await store.dispatch('login', {
            userInfo: userInfo,
            userType: userType.value,
            token: data.token
          })
          
          ElMessage.success('登录成功')
          
          // 根据用户类型跳转到相应页面
          if (userType.value === 'admin') {
            await router.push('/admin')
          } else {
            await router.push('/user')
          }
        } else {
          ElMessage.error(response?.message || '登录失败')
        }
        
      } catch (error) {
        console.error('登录异常:', error)
        if (error.code === 'ECONNREFUSED' || error.message.includes('Network Error')) {
          ElMessage.error('无法连接到服务器，请检查网络连接')
        } else if (error.response) {
          ElMessage.error(error.response.data?.message || '服务器错误')
        } else {
          ElMessage.error(error.message || '登录失败，请稍后重试')
        }
      } finally {
        loading.value = false
      }
    }

    const goToRegister = () => {
      router.push('/register')
    }

    const goToHome = () => {
      router.push('/')
    }

    return {
      loginFormRef,
      loading,
      userType,
      loginForm,
      loginRules,
      features,
      handleLogin,
      goToRegister,
      goToHome
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #667eea 100%);
  background-size: 400% 400%;
  animation: gradientShift 15s ease infinite;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  
  .floating-shapes {
    position: relative;
    width: 100%;
    height: 100%;
    
    .shape {
      position: absolute;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.1);
      animation: float 6s ease-in-out infinite;
      
      &.shape-1 {
        width: 80px;
        height: 80px;
        top: 10%;
        left: 10%;
        animation-delay: 0s;
      }
      
      &.shape-2 {
        width: 120px;
        height: 120px;
        top: 60%;
        right: 15%;
        animation-delay: 2s;
      }
      
      &.shape-3 {
        width: 60px;
        height: 60px;
        bottom: 20%;
        left: 20%;
        animation-delay: 4s;
      }
      
      &.shape-4 {
        width: 100px;
        height: 100px;
        top: 30%;
        right: 30%;
        animation-delay: 1s;
      }
    }
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.login-container {
  display: flex;
  width: 100%;
  max-width: 1100px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  box-shadow: 0 32px 64px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  animation: slideIn 0.8s ease-out;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(50px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.login-form {
  flex: 1;
  padding: 60px 50px;
  
  .form-header {
    text-align: center;
    margin-bottom: 40px;
    
    .logo-container {
      margin-bottom: 16px;
      
      .logo-icon {
        display: inline-block;
        width: 80px;
        height: 80px;
        background: linear-gradient(135deg, #1890ff, #36cfc9);
        border-radius: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 20px;
        animation: pulse 2s infinite;
        
        .el-icon {
          font-size: 40px;
          color: white;
        }
      }
    }
    
    .form-title {
      font-size: 32px;
      font-weight: 700;
      background: linear-gradient(135deg, #1890ff, #722ed1);
      background-clip: text;
      -webkit-background-clip: text;
      color: transparent;
      margin-bottom: 8px;
    }
    
    .form-subtitle {
      color: #666;
      font-size: 16px;
      font-weight: 500;
    }
  }
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

.user-type-container {
  margin-bottom: 32px;
  
  .user-type-selector {
    width: 100%;
    display: flex;
    background: #f5f5f5;
    border-radius: 12px;
    padding: 4px;
    
    .type-btn {
      flex: 1;
      border-radius: 8px;
      transition: all 0.3s ease;
      
      .el-radio-button__inner {
        width: 100%;
        border: none;
        background: transparent;
        padding: 16px 20px;
        font-weight: 500;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        border-radius: 8px;
        transition: all 0.3s ease;
        
        .el-icon {
          font-size: 18px;
        }
      }
      
      &.is-active {
        .el-radio-button__inner {
          background: linear-gradient(135deg, #1890ff, #36cfc9);
          color: white;
          box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
        }
      }
    }
  }
}

.input-container {
  margin-bottom: 24px;
  
  .custom-input {
    .el-input__wrapper {
      height: 56px;
      border-radius: 12px;
      background: rgba(255, 255, 255, 0.8);
      border: 2px solid #f0f0f0;
      transition: all 0.3s ease;
      padding: 0 20px;
      
      &:hover {
        border-color: #1890ff;
        background: white;
      }
      
      &.is-focus {
        border-color: #1890ff;
        background: white;
        box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
      }
      
      .el-input__inner {
        font-size: 16px;
        color: #333;
        
        &::placeholder {
          color: #999;
        }
      }
      
      .input-icon {
        font-size: 18px;
        color: #999;
        margin-right: 12px;
      }
    }
  }
}

.login-btn {
  width: 100%;
  height: 56px;
  border-radius: 12px;
  background: linear-gradient(135deg, #1890ff, #36cfc9);
  border: none;
  font-size: 18px;
  font-weight: 600;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  
  &:hover {
    background: linear-gradient(135deg, #40a9ff, #5cdbd3);
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(24, 144, 255, 0.3);
  }
  
  &:active {
    transform: translateY(0);
  }
}

.form-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 32px;
  
  .footer-btn {
    color: #666;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: all 0.3s ease;
    
    &:hover {
      color: #1890ff;
      transform: translateY(-1px);
    }
  }
}

.login-banner {
  flex: 1;
  background: linear-gradient(45deg, #1890ff, #36cfc9);
  color: white;
  padding: 60px 50px;
  display: flex;
  align-items: center;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="20" cy="20" r="1" fill="white" opacity="0.1"/><circle cx="80" cy="40" r="1" fill="white" opacity="0.1"/><circle cx="40" cy="80" r="1" fill="white" opacity="0.1"/></pattern></defs><rect width="100%" height="100%" fill="url(%23grain)"/></svg>');
    opacity: 0.3;
  }
  
  .banner-content {
    position: relative;
    z-index: 1;
    
    .banner-title {
      margin-bottom: 24px;
      
      h2 {
        font-size: 32px;
        font-weight: 700;
        margin-bottom: 16px;
        line-height: 1.2;
      }
      
      .title-line {
        width: 60px;
        height: 4px;
        background: white;
        border-radius: 2px;
      }
    }
    
    .banner-desc {
      font-size: 16px;
      opacity: 0.9;
      line-height: 1.6;
      margin-bottom: 48px;
    }
    
    .banner-features {
      .feature-item {
        display: flex;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 32px;
        transition: all 0.3s ease;
        
        &:hover {
          transform: translateX(8px);
        }
        
        .feature-icon {
          width: 48px;
          height: 48px;
          background: rgba(255, 255, 255, 0.2);
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-shrink: 0;
          
          .el-icon {
            font-size: 20px;
          }
        }
        
        .feature-text {
          h4 {
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 4px;
          }
          
          p {
            font-size: 14px;
            opacity: 0.8;
            line-height: 1.4;
            margin: 0;
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    max-width: 420px;
    margin: 20px;
  }
  
  .login-form {
    padding: 40px 30px;
    
    .form-header {
      .logo-container .logo-icon {
        width: 60px;
        height: 60px;
        
        .el-icon {
          font-size: 30px;
        }
      }
      
      .form-title {
        font-size: 24px;
      }
    }
  }
  
  .login-banner {
    padding: 40px 30px;
    text-align: center;
    
    .banner-content {
      .banner-title h2 {
        font-size: 24px;
      }
      
      .banner-features {
        .feature-item {
          justify-content: center;
          text-align: left;
          
          &:hover {
            transform: none;
          }
        }
      }
    }
  }
}
</style> 